package ch.subgate.pongwars;

import java.util.Random;

public class AIPlayer extends Player {

    public Ball ball;

    public int bounceSide;

    public int trickShot;

    public Random random;

    public AIPlayer(boolean isPlayerA, float size) {
        super(isPlayerA, size);
        trickShot = 2;
        bounceSide = 0;
        random = new Random();
        if (size == Constants.PADDLE_EASY) { mSpeed = Constants.AI_MOVE_SPEED_EASY; }
        else if (size == Constants.PADDLE_MEDIUM) { mSpeed = Constants.AI_MOVE_SPEED_MEDIUM; }
        else { mSpeed = Constants.AI_MOVE_SPEED_HARD; }
    } // Constructor

    public void setBall(Ball ball) {
        this.ball = ball;
    } // init

    @Override
    public void update(float deltaTime) {
        boolean isIncoming;

        // AI decisions
        isIncoming = ((mY == Constants.PLAYER_RED_YPOS) ? (ball.direction >= 0.0f) : (ball.direction < 0.0f));
        if (isIncoming) {
            if (bounceSide < 0) {
                if (ball.mX > mX) { mX += mSpeed * deltaTime; }
                else if (ball.mX < mX - mSize) { mX -= mSpeed * deltaTime; }
            } else if (bounceSide == 0) {
                if (ball.mX > mX + mSize) { mX += mSpeed * deltaTime; }
                else if (ball.mX < mX - mSize) { mX -= mSpeed * deltaTime; }
            } else {
                if (ball.mX > mX + mSize) { mX += mSpeed * deltaTime; }
                else if (ball.mX < mX) { mX -= mSpeed * deltaTime; }
            } // if
        } // if
        if (mX + mSize > Constants.DIC_BATTLEFIELD_WIDTH) { mX = Constants.DIC_BATTLEFIELD_WIDTH - mSize; }
        else if (mX - mSize < 0.0f) { mX = mSize; }
        super.update(deltaTime);
    } // update

    public void generatePlan() {
        bounceSide = random.nextInt(3) - 1;
        if (mSize == Constants.PADDLE_EASY) { trickShot = random.nextInt(11) - 5; }
        else if (mSize == Constants.PADDLE_MEDIUM) { trickShot = random.nextInt(7) - 3; }
        else { trickShot = random.nextInt(3) - 1; }
    } // generatePlan

    @Override
    public void reset() {
        // place the player in the middle of the battlefield
        mX = Constants.DIC_BATTLEFIELD_WIDTH / 2.0f;
    } // reset

} // AIPlayer
